package com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.dao.SprUucUrlPrivilegeDao;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.SprUucUrlPrivilegeService;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.DeleteSpModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.DeleteSrModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.GetSpModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.GetSrModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.ListSpModel;
import com.fosun.spr.plugin.uuc.api.sprUucUrlPrivilege.service.model.ListSrModel;
import com.fosun.spr.plugin.uuc.helper.mybatis.dao.SprUucUrlPrivilegeMapper;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUrlPrivilege;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUrlPrivilege;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucUrlPrivilegeExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class SprUucUrlPrivilegeServiceImpl extends CoBaseService implements
		SprUucUrlPrivilegeService {
	@Autowired 
	private SprUucUrlPrivilegeDao sprUucUrlPrivilegeDao;
	@Autowired
	private SprUucUrlPrivilegeMapper sprUucUrlPrivilegeMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(sprUucUrlPrivilegeDao.list(spModel));
		srModel.setTotalCount(sprUucUrlPrivilegeDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		SprUucUrlPrivilege db = sprUucUrlPrivilegeMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"链接权限配置");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprUucUrlPrivilege db = sprUucUrlPrivilegeMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		sprUucUrlPrivilegeMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getUrlPattern(),"链接地址（正则）");
		ExtAsserts.notNull(spModel.getPrivilegeId(),"权限");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		SprUucUrlPrivilegeExample eUrlPattern = new SprUucUrlPrivilegeExample();
		SprUucUrlPrivilegeExample.Criteria cUrlPattern = eUrlPattern.createCriteria();
		cUrlPattern.andUrlPatternEqualTo(spModel.getUrlPattern());
		cUrlPattern.andIsDeletedEqualTo(IS_DELETED_N);
		List<SprUucUrlPrivilege> rowsUrlPattern = sprUucUrlPrivilegeMapper.selectByExample(eUrlPattern);		
		
		if(StringUtils.isBlank(spModel.getId())){
			if(rowsUrlPattern.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUrlPattern()+" 已经存在");
			}		
			
			SprUucUrlPrivilege db = new SprUucUrlPrivilege();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			sprUucUrlPrivilegeMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsUrlPattern.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUrlPattern()+" 已经存在");
			}			
			if(rowsUrlPattern.size()==1 && !rowsUrlPattern.get(0).getUrlPattern().equals(spModel.getUrlPattern())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUrlPattern()+" 已经存在");
			}	
			
			SprUucUrlPrivilege db = sprUucUrlPrivilegeMapper.selectByPrimaryKey(spModel.getId());
			db.setUrlPattern(spModel.getUrlPattern());
			db.setPrivilegeId(spModel.getPrivilegeId());
			sprUucUrlPrivilegeMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
